'''
Created on 2021年1月18日

@author: Administrator
'''
from flask import json
from numpy.testing._private.parameterized import param
from peewee import JOIN, fn

from common.public import *
from orm.model import Service, HandleItem, ServiceDetail, db
from view.baseview import BaseView


class ServiceView(BaseView):

    def __init__(self):
        pass

    def getServiceName(self,):
        """取得用户列表"""
        result = []
        where_condition = True
        rows = Service.select().where(where_condition)
        for row in rows:
            data = {
                "id":row.id,
                "name":row.name,
               }
            result.append(data)
        return super().resultSet(200, "服务名称取得成功", result)

    def getServiceTree(self,):
        """取得服务树状图"""
        result = []
        where_condition = True
        rows = Service.select().where(where_condition)
        data = []
        for row in rows:
            node = {"id":row.id, "label":row.name, "children":[]}
            data.append(node)
        return super().resultSet(200, "服务名称取得成功", data)
    # def getHandleItem(self):
    #     """取得处理事项列表"""
    #     result = []
    #     where_condition = True
    #     rows = HandleItem.select().where(where_condition)
    #     for row in rows:
    #         data = {
    #             "id":row.id,
    #             "value":row.value,
    #             "service_id":row.service_id
    #            }
    #         result.append(data)
    #     return super().resultSet(200, "服务名称取得成功", result)

    def getServicesDetail(self, params):
        """取得服务明细，params：条件参数"""
        result = []
        rows = ServiceDetail.select().where(ServiceDetail.service_id == params["serviceId"]).paginate(int(params["pageNo"]), int(params["pagesize"]))
        for row in rows:
            data = {
                "id":row.id,
                "name":row.service.name,
                "spec":row.spec,
                "type":row.type,
                "unit":row.unit,
                "cost":row.cost,
                "price":row.price,
                "items":row.items,
                "countup":row.countup,
                "amountup":row.amountup,
                "number":row.number,
                "isactive": row.isactive,
                "memo":row.memo
             }
            result.append(data)
        return super().resultSet(200, "", result)

    def addServiceDetail(self, params):
        print(params)
        servicedetail = ServiceDetail.create(
            service_id = params["serviceId"],
            total_number = params["price"],
            spec = params["spec"],
            type = params["type"],
            cost = params["cost"],
            items = params["items"],
            price = params["price"],
            unit = params["unit"],
            countup = params["countup"],
            amountup = params["amountup"],
            number = params["number"],
            isactive = params["isactive"],
            memo = params["memo"]       
        )
        servicedetail.save()
        result = []
        return super().resultSet(200, "添加成功", result)

    def editServiceDetail(self, params):
        print(params)
        servicedetail = ServiceDetail.get(ServiceDetail.id == params["id"])
        servicedetail.cost = params["cost"]
        servicedetail.items = params["items"]
        servicedetail.memo = params["memo"]
        servicedetail.price = params["price"]
        servicedetail.spec = params["spec"]
        servicedetail.type = params["type"]
        servicedetail.unit = params["unit"]
        servicedetail.isactive = params["isactive"]
        servicedetail.countup = params["countup"]
        servicedetail.amountup = params["amountup"]
        servicedetail.number = params["number"]
        servicedetail.save()
        result = []
        return super().resultSet(200, "修改成功", result)
    
    def addService(self, params):
        print(params)
        service = Service.create(
            name = params["name"],
            time = params["time"],
            unit = params["unit"],
            memo = params["memo"],
            dept_id = params["dept_id"]       
        )
        service.save()
        result = []
        return super().resultSet(200, "添加成功", result)

    def editService(self, params):
        print(params)
        service = Service.get(Service.id == params["serviceId"])
        service.name = params["name"]
        service.save()
        result = []
        return super().resultSet(200, "修改成功", result)
